package io.renren.modules.app.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import io.renren.modules.app.annotation.Login;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.app.entity.AddressEntity;
import io.renren.modules.app.service.AddressService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;


/**
 * 用户地址表
 *
 * @date 2022-04-20 22:37:45
 */
@RestController
@RequestMapping("app/address")
public class AddressController {
    @Autowired
    private AddressService addressService;

    /**
     * 列表
     */
    @Login
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = addressService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{addressId}")
    @RequiresPermissions("generator:address:info")
    public R info(@PathVariable("addressId") Long addressId) {
        AddressEntity address = addressService.getById(addressId);

        return R.ok().put("address", address);
    }

    /**
     * 获取用户地址
     *
     * @param userId 用户id
     * @return
     */
    @GetMapping("getUserAddress/{userId}")
    @RequiresPermissions("generator:address:info")
    public R getUserAddress(@PathVariable("userId") Long userId) {
        AddressEntity userAddress = addressService.getUserAddress(userId);
        return R.ok().put("address", userAddress);
    }

    /**
     * 修改用户地址
     */
    @RequestMapping("/editUserAddress")
    @RequiresPermissions("generator:address:update")
    public R editUserAddress(@RequestBody AddressEntity address) {
        addressService.editUserAddress(address);

        return R.ok();
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("generator:address:save")
    public R save(@RequestBody AddressEntity address) {
        address.setCreateTime(new Date());
        addressService.save(address);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("generator:address:update")
    public R update(@RequestBody AddressEntity address) {
        addressService.updateById(address);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:address:delete")
    public R delete(@RequestBody Long[] addressIds) {
        addressService.removeByIds(Arrays.asList(addressIds));

        return R.ok();
    }

    /**
     * 获取用户地址
     *
     * @param userId 用户id
     * @return
     */
    @Login
    @GetMapping("queryUserAddress/{userId}")
    public R queryUserAddress(@PathVariable("userId") Long userId) {
        AddressEntity userAddress = addressService.getUserAddress(userId);
        return R.ok().put("userAddress", userAddress);
    }

    /**
     * 新增用户地址
     *
     * @param address 地址信息
     * @return
     */
    @Login
    @PostMapping("addAddress")
    public R addAddress(@RequestBody AddressEntity address){
        AddressEntity userAddress = addressService.getUserAddress(address.getUserId());
        if (userAddress!=null){
            addressService.editUserAddress(address);
        }else {
            addressService.addUserAddress(address);
        }

        return R.ok();
    }

}
